--[[=========================================================
	INCLUDES
=========================================================]]--
include( "shared.lua" )
include( "vgui/fonts.lua" )
include( "view/cl_calc_view.lua" )
include( "view/cl_crosshair.lua" )

--[[=========================================================
	GAMEMODE EVENTS
=========================================================]]--

--[[-----------------------------------------------------]]--
--	Initialization
--[[-----------------------------------------------------]]--
function GM:Initialize()

	--	Load config files
	self:LoadConfigFiles()
	
	--	Create fonts
	sc_vgui.CreateFonts()
	
	--	Prepare handlers
	net.Receive( "TestNetMsg", self.RcvTestNetMsg )
	
	--	Debug
	DEBUG:Msg( self.Name .. " has been initialized" )

end

--[[-----------------------------------------------------]]--
--	Computes the player view
--[[-----------------------------------------------------]]--
function GM:CalcView( ply, pos, angles, fov, nearZ, farZ )

	local view						= nil

	--	Get view cvars
	local firstPerson				= nil
	local thirdPerson				= nil
	if ply.ServerVars != nil then
		firstPerson					= ply.ServerVars[ "sc_allow_first_person" ]
		thirdPerson					= ply.ServerVars[ "sc_allow_third_person" ]
	end
	
	--	Get view values
	local firstAllowed				= nil
	local thirdAllowed				= nil
	if firstPerson then firstAllowed = firstPerson.Next end
	if thirdPerson then thirdAllowed = thirdPerson.Next end

	--	First person view
	if tobool( ply.FirstPerson ) then
	
		-- If first person is allowed then use it
		if tobool( firstAllowed ) then
			view					= self:CalcFirstPersonView( ply, pos, angles, fov, nearZ, farZ )
			
		--	If third person is allowed then use third person
		elseif tobool( thirdAllowed ) then
			ply.FirstPerson			= false
			view					= self:CalcThirdPersonView( ply, pos, angles, fov, nearZ, farZ )
		
		end
		
	--	Third person view
	elseif not tobool( ply.FirstPerson ) then
		
		--	If third person is allowed then use it
		if tobool( thirdAllowed ) then
			view					= self:CalcThirdPersonView( ply, pos, angles, fov, nearZ, farZ )
			
		--	If first person is allowed then use first person
		elseif tobool( firstAllowed ) then
			ply.FirstPerson			= true
			view					= self:CalcFirstPersonView( ply, pos, angles, fov, nearZ, farZ )
			
		end
	end
		
	--	Default calculation if none is allowed
	if view == nil then
		ply.FirstPerson				= true

		--	Setup a default 1st person view
		view						= {}
		view.origin					= pos
		view.angles					= angles
		view.fov					= fov
	end
	
	--	Get view model orientation & position
	local wep						= ply:GetActiveWeapon()
	if ply.FirstPerson and wep and IsValid( wep ) then
		local vlen					= ply:GetVelocity():Length()
		local footx					= ply.FootX or 0
		local footy					= ply.FootY or 0
		local sights				= false
		if wep.GetIronsights then
			sights					= wep:GetIronsights()
		end
		if sights then
			footx 					= footx / 2
			footy			 		= footy / 2
		end
		local fovp					= 1--( ( 90 / fov ) * 0.45 )
		local angof					= ( Angle( ( footx * 1.1 ) * vlen / 1000, ( footx * 1.7 ) * vlen / 1000, ( footy * 2 ) * vlen / 1000 ) ) * fovp
		pos							= pos + ( angles:Right() * ( footx * 1.2 or 0 ) + angles:Up() * ( footy * 1.05 or 0 ) ) * fovp
		angles						= angles - angof
		
		--	Try to set view model position and angles (this fails the first time)
		if wep.GetViewModelPosition then
			local vpos, vang	= wep:GetViewModelPosition( pos, angles )
			view.vm_origin		= ( view.vm_origin or Vector( 0, 0, 0 ) ) + vpos
			view.vm_angles		= ( view.vm_angles or Angle(0, 0, 0 ) ) + vang
		end
	end
	
	return view

end

--[[-----------------------------------------------------]]--
--	Determines whether player model should be drawn or not
--[[-----------------------------------------------------]]--
function GM:ShouldDrawLocalPlayer()

	return not LocalPlayer().FirstPerson

end

--[[-----------------------------------------------------]]--
--	HUD
--[[-----------------------------------------------------]]--
function GM:HUDPaint()

	--	Draw custom crosshair
	self:HUDCustomCrosshairDraw()

end

--[[-----------------------------------------------------]]--
--	Player used a command
--[[-----------------------------------------------------]]--
function GM:PlayerBindPress( ply, bind, press )

	--	Block multi-command binds
	if tobool( GetConVarNumber( "sc_block_multi_command_binds" ) ) then
		ply:PrintMessage( HUD_PRINTTALK, "Multi-command binding is considered cheating !" )
		return
	end

	--	Toggle first person view
	if bind == "impulse 100" and press then
		ply.FirstPerson	= not tobool( ply.FirstPerson )
	end

end